/**
 * 角色实体
 * 作者：GodMainCode
 * 创建时间：2024-01-17
 * 修改时间：2024-01-17
 * 修改人：GodMainCode
 */

import { 
  Entity, 
  Column, 
  PrimaryGeneratedColumn, 
  CreateDateColumn, 
  UpdateDateColumn,
  ManyToMany,
  JoinTable
} from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
import { IRole } from '@interfaces/role.interface';
import { User } from '../../user/entities/user.entity';
import { Permission } from '../../permission/entities/permission.entity';

@Entity('role')
export class Role implements IRole {
  @ApiProperty({ description: '角色ID' })
  @PrimaryGeneratedColumn({ comment: '角色ID' })
  id: number;

  @ApiProperty({ description: '角色名称' })
  @Column({ length: 50, unique: true, comment: '角色名称' })
  name: string;

  @ApiProperty({ description: '角色标识' })
  @Column({ length: 50, unique: true, comment: '角色标识' })
  code: string;

  @ApiProperty({ description: '角色描述' })
  @Column({ length: 200, nullable: true, comment: '角色描述' })
  description: string;

  @ApiProperty({ description: '排序' })
  @Column({ type: 'int', default: 0, comment: '排序' })
  sort: number;

  @ApiProperty({ description: '状态：0-禁用 1-启用' })
  @Column({ 
    type: 'smallint',
    default: 1,
    comment: '状态：0-禁用 1-启用'
  })
  status: number;

  @ApiProperty({ description: '创建时间' })
  @CreateDateColumn({ 
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
    comment: '创建时间'
  })
  created_at: Date;

  @ApiProperty({ description: '更新时间' })
  @UpdateDateColumn({ 
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
    comment: '更新时间'
  })
  updated_at: Date;

  @ApiProperty({ description: '是否删除：0-否 1-是' })
  @Column({ 
    type: 'smallint',
    default: 0,
    comment: '是否删除：0-否 1-是'
  })
  is_deleted: number;

  @ApiProperty({ description: '用户列表', type: () => [User] })
  @ManyToMany(() => User, user => user.roles)
  users: User[];

  @ApiProperty({ description: '权限列表', type: () => [Permission] })
  @ManyToMany(() => Permission, permission => permission.roles)
  @JoinTable({
    name: 'role_permission',
    joinColumn: { name: 'role_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'permission_id', referencedColumnName: 'id' },
  })
  permissions: Permission[];
} 